<?php require_once('Connections/sqlaerosnemanje.php'); ?>
<?php 
header("Content-type: text/xml");

$selected_projekt = $_POST['selectProjekt'];
$selected_st_reda = $_POST['selectRed'];

mysql_select_db($database_sqlaerosnemanje, $sqlaerosnemanje);
$query_izberiTocke = "SELECT * FROM tocke_geodetic WHERE stReda = '$selected_st_reda' AND IDProjekta = '$selected_projekt' ORDER BY imeTocke";
$izberiTocke = mysql_query($query_izberiTocke, $sqlaerosnemanje) or die(mysql_error());
$totalRows_izberiTocke = mysql_num_rows($izberiTocke);

mysql_select_db($database_sqlaerosnemanje, $sqlaerosnemanje);
$query_izberiParametre = "SELECT * FROM xml_parameters WHERE IDProjekta = '$selected_projekt' AND stReda = '$selected_st_reda'";
$izberiParametre = mysql_query($query_izberiParametre, $sqlaerosnemanje) or die(mysql_error());
$row_izberiParametre = mysql_fetch_assoc($izberiParametre);
$totalRows_izberiParametre = mysql_num_rows($izberiParametre);

mysql_select_db($database_sqlaerosnemanje, $sqlaerosnemanje);
$query_PrviZadnji = "SELECT MIN(Y) as cmin_y, MIN(X) as cmin_x, MAX(Y) as cmax_y, MAX(X) as cmax_x FROM tocke_grid WHERE tocke_grid.IDProjekta = '$selected_projekt' AND tocke_grid.stReda = '$selected_st_reda'";
$PrviZadnji = mysql_query($query_PrviZadnji, $sqlaerosnemanje) or die(mysql_error());
$row_PrviZadnji = mysql_fetch_assoc($PrviZadnji);
$totalRows_PrviZadnji = mysql_num_rows($PrviZadnji);

// Racunanje smeri letenja za posamezen red
$y_min = $row_PrviZadnji['cmin_y'];
$x_min = $row_PrviZadnji['cmin_x'];
$y_max = $row_PrviZadnji['cmax_y'];
$x_max = $row_PrviZadnji['cmax_x'];

$x = $x_max - $x_min;
$y = $y_max - $y_min;

if($x == 0) $x = 1 / 10000;
     
$deg = rad2deg(atan(abs($y / $x)));
     
if($y >= 0)
{
   $deg = $x < 0 ? 180 - $deg : $deg;
}
else
{
  $deg = $x < 0 ? 180 + $deg : 360 - $deg;
}



$paramDirection = $row_izberiParametre['direction'];
$paramLineLenght = $row_izberiParametre['line_lenght'];

$xml_output = "<?xml version=\"1.0\"?>\n";
$xml_output .= "<shoot version=\"1\">\n";
$xml_output .= "<parameters cone=\"45\" direction=\"".$paramDirection."\" error=\"30\" format=\"dec\"\n";
$xml_output .= "line_length=\"".$paramLineLenght."\" radius=\"1500\"/>\n";
$xml_output .= "\t<target-list>\n";

for($x = 0; $x < $totalRows_izberiTocke; $x++){
	$row_izberiTocke = mysql_fetch_assoc($izberiTocke);
    if ($row_izberiTocke['guidelineTocka'] != "1")
    {
    	$xml_output .= "\t\t<target lat=\"".round($row_izberiTocke['lat'], 6)."\" lon=\"".round($row_izberiTocke['lon'], 6)."\">\n"; 
    	$xml_output .= "\t\t</target>\n";
  	}
  	else
  	{
  		$xml_output .= "\t\t<target guide_line=\"yes\" lat=\"".round($row_izberiTocke['lat'], 6)."\" lon=\"".round($row_izberiTocke['lon'], 6)."\">\n"; 
    	$xml_output .= "\t\t</target>\n";
  	}
}
$xml_output .= "\t</target-list>\n";
$xml_output .= "</shoot>";

echo $xml_output;
/*
mysql_query("DELETE FROM `aerosnemanje`.`tocke_plan` WHERE `UID` > 0", $linkID) or die (mysql_error());
*/
mysql_free_result($izberiTocke);

mysql_free_result($izberiParametre);

mysql_free_result($PrviZadnji);
?>
